package com.spsoft.vatoutinv.service.matchflow.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spsoft.core.enums.ResponseEnum;
import com.spsoft.core.model.Result;
import com.spsoft.core.utils.BeanUtils;
import com.spsoft.system.entity.NvatOutInvoiceDetails;
import com.spsoft.system.entity.NvatOutInvoiceItems;
import com.spsoft.vatoutinv.entity.matchflow.VmsMatching;
import com.spsoft.vatoutinv.entity.matchflow.VmsMatchingDetail;
import com.spsoft.vatoutinv.enums.CommonEnum;
import com.spsoft.vatoutinv.mapper.VmsMatchingMapper;
import com.spsoft.vatoutinv.model.dto.matchflow.VmsMatchFlowPageDto;
import com.spsoft.vatoutinv.model.vo.matchflow.VmsMatchFlowApplyVo;
import com.spsoft.vatoutinv.model.vo.matchflow.VmsMatchInvoiceDetailVo;
import com.spsoft.vatoutinv.model.vo.matchflow.VmsMatchRecordVo;
import com.spsoft.vatoutinv.service.matchflow.VmsMatchingService;
import com.spsoft.workflow.service.impl.ProcessBusinessServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 手工匹配流水表 服务实现类
 * </p>
 *
 * @author chenxudong
 * @since 2024-06-25
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class VmsMatchingServiceImpl extends ServiceImpl<VmsMatchingMapper, VmsMatching> implements VmsMatchingService {

    private final ProcessBusinessServiceImpl processBusinessService;

    @Override
    public IPage<VmsMatchFlowApplyVo> pageApply(Page<VmsMatchFlowApplyVo> page, VmsMatchFlowPageDto pageDto) {
        IPage<VmsMatchFlowApplyVo> matchFlowVoIPage = baseMapper.pageApply(page, pageDto);
        // 发票匹配流水 需要实时计算出税额差异
        if (CommonEnum.MATCH_FLOW_TYPE_0.getKey().equals(pageDto.getApplyType())
            && CollectionUtil.isNotEmpty(matchFlowVoIPage.getRecords())) {
            for (VmsMatchFlowApplyVo vmsMatchFlowVo : matchFlowVoIPage.getRecords()) {
                // 税额差异 = 发票税额 - 流水税额
                vmsMatchFlowVo.setSecy(vmsMatchFlowVo.getSe().subtract(vmsMatchFlowVo.getLsse()));
            }
        }
        return matchFlowVoIPage;
    }

    @Override
    public List<VmsMatchFlowApplyVo> getApplyData(VmsMatchFlowPageDto pageDto) {
        List<VmsMatchFlowApplyVo> matchFlowList = baseMapper.getApplyData(pageDto);
        // 发票匹配流水 需要实时计算出税额差异
        if (CommonEnum.MATCH_FLOW_TYPE_0.getKey().equals(pageDto.getApplyType())
                && CollectionUtil.isNotEmpty(matchFlowList)) {
            for (VmsMatchFlowApplyVo vmsMatchFlowVo : matchFlowList) {
                // 税额差异 = 发票税额 - 流水税额
                vmsMatchFlowVo.setSecy(vmsMatchFlowVo.getSe().subtract(vmsMatchFlowVo.getLsse()));
            }
        }
        return matchFlowList;
    }

    @Override
    public Result<Object> judgeProcessIsFinish(String matchFlowType, String fpdm, String fphm, String deptCode, Long applyId, Long oidid) {
        // 1.判断数电系统是否有未结束的流程
        List<Long> list = baseMapper.judgeProcessIsFinish(matchFlowType, fpdm, fphm, deptCode, applyId);
        if (CollectionUtil.isNotEmpty(list)) {
            if (CommonEnum.MATCH_FLOW_TYPE_0.getKey().equals(matchFlowType)) {
                return Result.ERROR(ResponseEnum.MATCH_FLOW_PROCESS_NOT_FINISH_ERROR);
            } else {
                return Result.ERROR(ResponseEnum.MATCH_FLOW_REVOKE_PROCESS_NOT_FINISH_ERROR);
            }
        }

        // 2.判断老系统是否有未结束的流程
        if (CommonEnum.MATCH_FLOW_TYPE_0.getKey().equals(matchFlowType)) {
            // 发票匹配流水
            List<String> idList = baseMapper.judgeOldMatchIsFinish(oidid);
            if (CollectionUtil.isNotEmpty(idList)) {
                return Result.ERROR(ResponseEnum.OLD_MATCH_FLOW_PROCESS_NOT_FINISH_ERROR);
            }
        } else {
            // 发票匹配流水撤销
            List<Long> idList = baseMapper.judgeOldRevokeIsFinish(oidid);
            if (CollectionUtil.isNotEmpty(idList)) {
                return Result.ERROR(ResponseEnum.OLD_MATCH_FLOW_REVOKE_PROCESS_NOT_FINISH_ERROR);
            }
        }

        return Result.OK();
    }

    @Override
    public VmsMatchRecordVo getMatchDetailById(Long matid) {
        VmsMatchRecordVo vmsMatchRecordVo = baseMapper.getMatchDetailById(matid);
        if (vmsMatchRecordVo != null) {
            // 实时计算金额差异、税额差异
            if (ObjectUtil.isNotEmpty(vmsMatchRecordVo.getFpje()) && ObjectUtil.isNotEmpty(vmsMatchRecordVo.getLsje())) {
                // 金额差异 = 发票金额 - 流水金额
                vmsMatchRecordVo.setJecy(vmsMatchRecordVo.getFpje().subtract(vmsMatchRecordVo.getLsje()));
            }
            if (ObjectUtil.isNotEmpty(vmsMatchRecordVo.getFpse()) && ObjectUtil.isNotEmpty(vmsMatchRecordVo.getLsse())) {
                // 税额差异 = 发票税额 - 流水税额
                vmsMatchRecordVo.setSecy(vmsMatchRecordVo.getFpse().subtract(vmsMatchRecordVo.getLsse()));
            }
        }

        return vmsMatchRecordVo;
    }

    @Override
    public VmsMatchInvoiceDetailVo convertToDetailVo(NvatOutInvoiceDetails nvatOutInvoiceDetail, Long matid) {
        VmsMatchInvoiceDetailVo invoiceDetailVo = new VmsMatchInvoiceDetailVo();
        invoiceDetailVo.setMatid(matid);
        invoiceDetailVo.setXsfmc(nvatOutInvoiceDetail.getKpdw());
        invoiceDetailVo.setFplx(nvatOutInvoiceDetail.getFplxdm());
        invoiceDetailVo.setFpdm(nvatOutInvoiceDetail.getFpdm());
        invoiceDetailVo.setFphm(nvatOutInvoiceDetail.getFphm());
        invoiceDetailVo.setKprq(nvatOutInvoiceDetail.getKprq());
        invoiceDetailVo.setGmfmc(nvatOutInvoiceDetail.getSpfmc());
        invoiceDetailVo.setGmfsbh(nvatOutInvoiceDetail.getSpfsbh());
        invoiceDetailVo.setHjje(nvatOutInvoiceDetail.getHjje());
        invoiceDetailVo.setHjse(nvatOutInvoiceDetail.getHjse());
        invoiceDetailVo.setJshj(nvatOutInvoiceDetail.getJshj());
        invoiceDetailVo.setYwdhlx(nvatOutInvoiceDetail.getYwdhlx());
        invoiceDetailVo.setYwdh(nvatOutInvoiceDetail.getYwdh());

        return invoiceDetailVo;
    }

    @Override
    public VmsMatchRecordVo.VmsMatchItemRecordVo buildMatchItemRecord(NvatOutInvoiceItems nvatOutInvoiceItems, List<VmsMatchingDetail> matchDetailList) {
        VmsMatchRecordVo.VmsMatchItemRecordVo vmsMatchItemRecordVo = new VmsMatchRecordVo.VmsMatchItemRecordVo();
        BeanUtils.copyProperties(nvatOutInvoiceItems, vmsMatchItemRecordVo);

        // 计算流水总金额、流水总税额
        BigDecimal lszje = BigDecimal.ZERO, lszse = BigDecimal.ZERO;
        if (CollectionUtil.isNotEmpty(matchDetailList)) {
            for (VmsMatchingDetail vmsMatchingDetail : matchDetailList) {
                lszje = lszje.add(vmsMatchingDetail.getLsje());
                lszse = lszse.add(vmsMatchingDetail.getLsse());
            }
            vmsMatchItemRecordVo.setLszje(lszje);
            vmsMatchItemRecordVo.setLszse(lszse);
        }

        // 计算金额差异、税额差异
        vmsMatchItemRecordVo.setJecy(nvatOutInvoiceItems.getHsje().subtract(lszje));
        vmsMatchItemRecordVo.setSecy(nvatOutInvoiceItems.getSe().subtract(lszse));

        return vmsMatchItemRecordVo;
    }
}
